using System;
using System.ComponentModel;
using System.IO;
using System.Text;
using RollManager.Library.Utils;

namespace RollManager.Library.ActionSteps
{
    [Serializable]
    public class TouchFilesStep : ActionStep
    {
        #region Properties
        private string m_destDir = StepConstants.PATH;
        private string m_filter = StepConstants.FILTER;
        private bool m_recursive = false;

        [NonSerialized]
        private DateTime m_timeStamp;

        [DisplayName( "Destination Dir" )]
        [Description( "The destination directory path to which time stamps are applied." )]
        public string DestDir
        {
            get
            {
                return m_destDir;
            }
            set
            {
                if( string.IsNullOrEmpty( value ) )
                {
                    throw new ArgumentException( "Destination Dir cannot be empty." );
                }
                else
                {
                    m_destDir = value.Trim();
                }
            }
        }

        [DisplayName( "File Filter" )]
        [Description( "The file filter to use when applying time stamps to files in the destination." )]
        public string Filter
        {
            get
            {
                return m_filter;
            }
            set
            {
                if( string.IsNullOrEmpty( value ) )
                {
                    throw new ArgumentException( "Filter cannot be empty.  Use *.* for a global wildcard." );
                }
                else
                {
                    m_filter = value.Trim();
                }
            }
        }

        [Description( "Whether the copy should only apply to the top level destination directory or to all children directories as well." )]
        public bool Recursive
        {
            get
            {
                return m_recursive;
            }
            set
            {
                m_recursive = value;
            }
        }

        public override string Name
        {
            get { return "Touch Files"; }
        }
        #endregion

        protected override void Run()
        {
            if( FirstServer == CurrentServer )
            {
                m_timeStamp = DateTime.Now;
            }

            Simulate();

            DirectoryInfo dir = new DirectoryInfo( m_destDir );
            touchDirectory( dir );
        }

        protected override void Simulate()
        {
            if( !Directory.Exists( m_destDir ) )
            {
                throw new Exception( string.Format( "Destination directory {0} does not exist", m_destDir ) );
            }
        }

        private void touchDirectory( DirectoryInfo dir )
        {
            if( m_recursive )
            {
                foreach( DirectoryInfo subDir in dir.GetDirectories() )
                {
                    touchDirectory( subDir );
                }
            }

            foreach( FileInfo file in dir.GetFiles( m_filter ) )
            {
                file.CreationTime = m_timeStamp;
                file.LastWriteTime = m_timeStamp;
                file.LastAccessTime = m_timeStamp;
            }

            dir.CreationTime = m_timeStamp;
            dir.LastWriteTime = m_timeStamp;
            dir.LastAccessTime = m_timeStamp;
        }
    }
}
